package com.rran.study.concurrent.base;

import com.rran.study.concurrent.SleepTools;

/**
 * @author yy
 * @Type UseJoin.java
 * @Desc 演示Join()方法的使用
 * @date 2020/7/29 14:38
 */
public class UseJoin {

    static class WangWu implements Runnable {
        private Thread thread;

        public WangWu(Thread thread) {
            this.thread = thread;
        }

        public WangWu() {
        }

        @Override
        public void run() {
            System.out.println("WangWu开始排队打饭.....");
            try {
                if(thread!=null) {
                    thread.join();
                }
            } catch (InterruptedException e) {
            }
            SleepTools.second(2);//休眠2秒
            System.out.println(Thread.currentThread().getName()
                    + " WangWu打饭完成.");
        }
    }

    static class Lisi implements Runnable {
        @Override
        public void run() {
            SleepTools.second(2);//休眠2秒
            System.out.println("Lisi开始排队打饭.....");
            System.out.println(Thread.currentThread().getName()
                    + " Lisi打饭完成.");
        }
    }

    public static void main(String[] args) throws Exception {

        Thread zhangsan = Thread.currentThread();
        Lisi lisi = new Lisi();
        Thread lisiThread = new Thread(lisi);
        WangWu wangWu = new WangWu(lisiThread);
        Thread wangWuThread = new Thread(wangWu);
        lisiThread.start();
        wangWuThread.start();
        System.out.println("zhangsan开始排队打饭.....");
        lisiThread.join();
        SleepTools.second(2);//让主线程休眠2秒
        System.out.println(Thread.currentThread().getName() + " zhangsan打饭完成.");
    }
}
